<?php

/** @var Factory $factory */

use App\FreeCar;
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factory;
use Illuminate\Support\Facades\Date;

$factory->define(FreeCar::class, function (Faker $faker) {
    // 假设 $freeCarList 是一个关联数组的集合
    $freeCarList = [];

// 定义随机选择的数组
    $licensePlates = [
        '鄂ARB989',
        '鄂A7W16R',
        '鄂AF156S',
        '鄂AQ5300',
        '鄂AQ5111'
    ];

    $constructionUnits = [
        '工程部',
        '重庆智翔',
        '江苏中压',
        '重庆交科',
        '鑫政顺畅'
    ];

    $vehicleTypes = [
        '轻型自卸货车',
        '中型载货专项作业车',
        '轻型自卸货车',
        '轻型自卸货车',
        '中型非载货专项作业车'
    ];

    $currentDate = Date::now();
    // 从车牌数组中随机选择一个车牌索引
    $randomLicensePlateIndex = array_rand($licensePlates);
    // 从建设单位数组中随机选择一个建设单位索引
    $randomConstructionUnitIndex = array_rand($constructionUnits);
    // 从车辆类型数组中随机选择一个车辆类型索引
    $randomVehicleTypeIndex = array_rand($vehicleTypes);

// 循环生成数据

    // 生成模拟的30条免费车辆数据

    // 随机选择车牌、建设单位和车辆类型
    $licensePlate = $licensePlates[$randomLicensePlateIndex];
    $constructionUnit = $constructionUnits[$randomConstructionUnitIndex];
    $vehicleType = $vehicleTypes[$randomVehicleTypeIndex];

    // 随机生成一个6到20周的时段，分别计算免费时段的开始和结束日期
    $freeTimeStart = $currentDate->subWeeks(random_int(6, 20))->format('Y-m-d');
    $freeTimeEnd = $currentDate->addWeeks(random_int(6, 20))->format('Y-m-d');
    // 确保免费时段的开始日期不晚于结束日期
    if ($freeTimeEnd < $freeTimeStart) {
        $temp = $freeTimeEnd;
        $freeTimeEnd = $freeTimeStart;
        $freeTimeStart = $temp;
    }

    // 生成并累积每辆车的详细信息数组，包括id、车牌、建设单位、免费时段等
    $freeCarsData = [

        'licensePlate' => $licensePlate,
        'constructionUnit' => $constructionUnit,
        'freeTimeStart' => $freeTimeStart,
        'freeTimeEnd' => $freeTimeEnd,
        'freeSection' => '黄石-散花',
        'vehicleType' => $vehicleType,
        'theValidityPeriodOfTheAnnualInspectionOfTheVehicle' => $currentDate->addWeeks(random_int(6, 50))->format('Y-m-d'),
        'validityPeriodOfVehicleInsurance' => $currentDate->addWeeks(random_int(6, 50))->format('Y-m-d'),
        'updatedDate' => $currentDate->format('Y-m-d'),
        'remark' => generateRandomChineseString(20),
    ];

    return $freeCarsData;
});
function generateRandomChineseString($length = 10): string
{
    // 定义中文字符的 Unicode 范围
    $low = 0x4E00; // 起始码位
    $high = 0x9FA5; // 结束码位

    $string = '';

    for ($i = 0; $i < $length; $i++) {
        // 生成一个随机的 Unicode 码位
        $charCode = mt_rand($low, $high);

        // 将码位转换成 UTF-8 字符
        $char = mb_convert_encoding(pack('H*', sprintf('%04X', $charCode)), 'UTF-8', 'UCS-2');

        $string .= $char;
    }

    return $string;
}
